/////////////////////////////////////////////////////////////////////////////
//
// MdtLogConsole.cpp
//
// Author : L.F.
// Date   : 2006.10.17
//
/////////////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "../Xepdtlib/XepdtLogConsole.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CMdtLogConsole

/////////////////////////////////////////////////////////////////////////////
// CMdtLogConsole

CXepdtLogConsole::CXepdtLogConsole()
{
	m_bInit = FALSE;

	m_nIndex  = 0;
	m_hStdOUt = NULL;
}

CXepdtLogConsole::~CXepdtLogConsole()
{
	FreeConsole();
}


/////////////////////////////////////////////////////////////////////////////
// CMdtLogConsole method implementation

BOOL CMdtLogConsole::Init()
{
	OSVERSIONINFO osVer;
	osVer.dwOSVersionInfoSize = sizeof( osVer );
	GetVersionEx( & osVer );

	if ( osVer.dwPlatformId == VER_PLATFORM_WIN32s )
	{
		m_bInit = FALSE;
		return FALSE;
	}

	if ( ! AllocConsole() )
	{
		m_bInit = FALSE;
		return FALSE;
	}

	SetConsoleTitle( _T( "Mdt" ) );
	m_hStdOUt = GetStdHandle( STD_OUTPUT_HANDLE );
	ASSERT( m_hStdOUt );

	m_bInit = TRUE;

	return TRUE;
}

VOID CMdtLogConsole::OutputMdtInfo( CString strInfo, TEXT_COLOR textColor )
{
	if ( ! m_bInit )
		return;

	// CleanConsoleScreen();

	m_nIndex++;

	CString strOutputInfo;
	strOutputInfo.Format( "<%05d> %s \n\n", m_nIndex, strInfo );

	DWORD dwCount;

	ASSERT( m_hStdOUt );
	SetConsoleTextAttribute( m_hStdOUt, textColor );
	WriteConsole( m_hStdOUt, strOutputInfo, strOutputInfo.GetLength(), & dwCount, NULL );

	/////////////////////////////////////////////////////////////////////////
	// static std::string log;
	// log += strOutputInfo.operator LPCTSTR();
	// log += "\n";
	
	// CStdioFile file;
	// if ( ! file.Open( "C:\\MdtLog.txt", CFile::modeCreate | CFile::modeWrite ) )
	// 	return;
	
	// file.WriteString( log.c_str() );
	// file.Close();
	/////////////////////////////////////////////////////////////////////////
}

VOID CMdtLogConsole::CleanConsoleScreen()
{
	if ( ! m_bInit )
		return;

	ASSERT( m_hStdOUt );

	CONSOLE_SCREEN_BUFFER_INFO csbi;
	GetConsoleScreenBufferInfo( m_hStdOUt, & csbi );

	DWORD dwConSize   = csbi.dwSize.X * csbi.dwSize.Y;
	DWORD dwWritten   = 0;

	COORD coordScreen = { 0, 0 };

	FillConsoleOutputCharacter( m_hStdOUt, ( TCHAR ) ' ', dwConSize, coordScreen, & dwWritten );

	GetConsoleScreenBufferInfo( m_hStdOUt, & csbi );
	FillConsoleOutputAttribute( m_hStdOUt, csbi.wAttributes, dwConSize, coordScreen, & dwWritten );
	SetConsoleCursorPosition( m_hStdOUt, coordScreen );
}